//
// Created by gogoqiu on 2021/9/23.
//
#include "MyTagApp.h"
#include "PreferenceCore.h"

/**
 * 数据库
 * @param _myTagApp
 */
PreferenceCore::PreferenceCore( MyTagApp * _myTagApp ){

    this->myTagApp = _myTagApp;

    if( _myTagApp->getAppTag().empty() ){
        //throw std::exception( "App Tag can't be empty!" );
        throw ( "App Tag can't be empty!" );
    }
    //set the storage, and select the items by app tag
    //storage
    storage = new SqlitePreferenceStorage( _myTagApp );
}

void PreferenceCore::adapter(){
    /*
     * myTagApp::setPreferenceCore
     *  PreferenceCore
     *
     * */
    if( storage->Load()==-1 ){
        throw ( "Preference load error!" );
    }
    //
    buildList();
    //printf( "数据库存储配置项数目:%zu\n", storage->getPreferences().size() );
    //
    buildViewList();
}

PreferenceCore::~PreferenceCore(){
    //if( storage )
    delete storage;
    std::vector<ViewList* >::iterator iterator;
    for( iterator= list.begin(); iterator!=list.end();++iterator ){
        delete iterator.operator*();
    }
}

/*
 * App:
 * view端:core->storage(App_Tag)->ViewItem->database
 * Editor:
 * view端:
 *
*/
ViewList* PreferenceCore::getTitle( const wxString& key ){
    std::vector<ViewList* >::iterator iterator;
    for( iterator= list.begin(); iterator!=list.end();++iterator ){
        if( iterator.operator*()->key.IsSameAs( key )){
            return iterator.operator*();
        }
    }
    return nullptr;
}

//by storage to get view items
PreferenceView* PreferenceCore::getViewItem( PreferenceItem * item ){
    ViewList* viewList = getTitle( item->getKey() );
    if( viewList ){
        return new PreferenceView( item, viewList->type, viewList->title ) ;
    }
    return nullptr;
}

/*
 *
 */
void PreferenceCore::buildViewList(){
    PreferenceView * preferenceView;
    /*
     * from database list
     *
     */
    vector<PreferenceItem*> preferences = storage->getPreferences();
    vector<PreferenceItem*>::iterator _iterator;

    for(_iterator = preferences.begin();_iterator!=preferences.end(); _iterator++ ) {
        //
        preferenceView = getViewItem( _iterator.operator*() );
        if( preferenceView ) {
            //_iterator.
            this->preferenceViews.push_back(preferenceView);
        }
    }
    //
    printf( "数据库存储配置项数目:%zu\n", this->preferenceViews.size() );
}

/**
 * for extend preference to query the value of special key
 * @param key
 * @return
 */
wxString PreferenceCore::QueryValue( const wxString& key ) const{
    //update current value
    storage->Load();
    vector<PreferenceItem*> preferences = storage->getPreferences();
    vector<PreferenceItem*>::iterator _iterator;
    wxString value;
    bool found = false;
    for(_iterator = preferences.begin();_iterator!=preferences.end(); _iterator++ ) {
        if( _iterator.operator*()->getKey().IsSameAs( key ) ){
            //null?
            if( _iterator.operator*()->getValueIsNull()){
                value = _iterator.operator*()->getDefaultValue();
            }else
                value = _iterator.operator*()->getValue();
            break;
        }
    }
    return value;
}
/*
wxString PreferenceCore::get_my_key1(){
    return QueryValue( KEY_MY_KEY1 );
}
wxString PreferenceCore::get_my_key2(){
    return QueryValue( KEY_MY_KEY2 );
}
*/

/**
 *
 * @param key
 * @param value
 * @return
 */
int PreferenceCore::PutValue( const wxString& key, const wxString& value  ) const{
    return storage->updateItem( key, value );
}

/*
 * support zero
 * */
vector<PreferenceView*> PreferenceCore::getPreferenceViews(){
    return preferenceViews;
};